{include file="default/base/seller_top" /}
<div class="seller_main w1200">
    {include file="default/base/seller_left" /}
    <div class="seller_right">


        <div class="seller_items">
            {include file="default/base/seller_items" /}
        </div>
        {if isset($edit_goods_sign)}
        {else /}
        <ul class="add-goods-step">
            <li><i class="icon iconfont">&#xe600;</i>
                <h6>STEP.1</h6>
                <h2>{$Think.lang.select_category}</h2>
                <i class="arrow iconfont">&#xe687;</i></li>
            <li><i class="icon iconfont">&#xe731;</i>
                <h6>STEP.2</h6>
                <h2>填写基本信息</h2>
                <i class="arrow iconfont">&#xe687;</i></li>
            <li class="current"><i class="icon iconfont">&#xe731;</i>
                <h6>STEP.3</h6>
                <h2>完善商品信息</h2>
                <i class="arrow iconfont">&#xe687;</i></li>
            <li><i class="icon iconfont">&#xe6a2;</i>
                <h6>STEP.4</h6>
                <h2>{$Think.lang.store_goods_index_upload_goods_pic}</h2>
        </ul>
        {/if}
        <form method="post" id="goods_image"  action="{:url('Sellergoodsadd/add_step_four',['commonid'=>$commonid])}">
            <input type="hidden" name="commonid" id="commonid" value="{$commonid}">
            <input type="button" class="submit" value="批量完善信息" style="margin-left: 728px;" id="p_w"/>
            <a href="javascript:void(0);" class="dssc-btn dssc-btn-acidblue"
               style="margin-top: -54px;    margin-left: 859px;" id="selectArea" action="{:url('Sellergoodsadd/save_area')}"><p>批量设置销售区域</p></a>
            <input type='hidden' name='area_id' value="{$goods.area|default=''}" data_name='' id='area_id_list'>
            <input type='hidden' name='area_name' value="{$goods.area_name|default=''}">

            <table class="dssc-default-table">
                <thead>
                <tr>
                    <th class="w50"><input type="checkbox" name="" id="all">全选</th>
                    <th class="w50">序号</th>
                    <th class="w200">商品名及规格</th>
                    <th class="w100">是否已经完善信息</th>
                    <th class="w100">是否设置销售区域</th>
                    <th class="w200">操作</th>
                </tr>
                </thead>
                <tbody>
                {if $goods_info}
                <?php $index=1; foreach($goods_info as $k=>$v){ ?>
                <tr class="bd-line">
                    <td><input type="checkbox" name="" class="checkboxs" value="{$v.goods_id}"></td>
                    <td><?php echo $index;?></td>
                    <td>{$v.goods_name}</td>
                    <td class="is_perfect"><?php if(empty($v['is_w_v'])){ echo '未完善';}else{echo '已完善'; } ?> </td>
                    <td class="is_save_area" ><?php if(empty($v['area'])){ echo '未设置';}else{echo '已设置'; } ?></td>
                    <td style="display: flex;align-items: center; justify-content: center;">
                        <a href="javascript:void(0);" class="dssc-btn dssc-btn-green" style="margin-right: 10px;"
                           onclick="goods_setting_info({$v['goods_id']});"><p>查看详情</p></a>
                        <input type="button" class="submit" onclick="perfect_goods({$v.goods_id});" value="完善信息"
                               style="margin-right: 10px;display: inline-block;"/>
                        <a href="javascript:void(0);" class="dssc-btn dssc-btn-acidblue" style="margin-right: 10px;"  ><p class="save_area" action="{:url('Sellergoodsadd/save_area')}" id="{$v['goods_id']}">设置销售区域</p></a>
                        
                    </td>
                </tr>
                <?php $index++; } ?>
                {/if}
                </tbody>
            </table>

            <!--弹框地区部分代码-->
            <div class="list" style='display:none;margin-left: 20px;' id="allsa">
                &nbsp;&nbsp;&nbsp;<input style="margin-right: 8px;" type="checkbox" id="checkboxs" name="checkall">全选
                <?php foreach($top_area as $k=>$v){ ?>
                <div class="top">

                    &nbsp;&nbsp;&nbsp; <span style="font-family: 'dtreefont' !important;font-size: 16px;font-style: normal;-webkit-font-smoothing: antialiased;-webkit-tap-highlight-color: rgba(0,0,0,0);"
          class='select_choose dtreefont dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon' data-spread="close">
    </span>
                    <input style="margin-right: 8px;" type="checkbox" value="<?php echo $v['area_id']; ?>" class="top"
                           area_deep="1" ids="<?php echo $v['last_ids']; ?>" data_name="<?php echo $v['area_name']; ?>"><?php echo $v['area_name']; ?>
                    <div class="sec<?php echo $v['area_id']; ?>" style='padding-left:20px; margin:10px;'></div>
                </div>
                <?php }?>
            </div>

            <div class="bottom tc hr32"> <?php if (!isset($edit_goods_sign)) { ?>
                <input type="button"  class="dssc-btn dssc-btn-acidblue"  style="margin-top: 10px;width: 260px;margin-left: -397px;height: 40px;font-size: 16px;"
                       value="暂存商品信息" id="zan_submit" action="{:url('Sellergoodsinfomessage/perfect_goods_list')}" />

                <input type="button" class="submit" lay-filter="formDemo" style="margin-left: 548px;margin-top: -39px;"
                       value="<?php if (isset($edit_goods_sign)) { echo lang('ds_submit'); } else { ?>{$Think.lang.store_goods_add_next}，上传商品图片<?php }?>"
                       id='button_submit' />
                <?php }else{ ?>
                <input type="button"  class="dssc-btn dssc-btn-acidblue"  style="margin-top: 10px;width: 260px;margin-left: -105px;height: 40px;font-size: 16px;"
                       value="暂存商品信息" id="zan_submit" action="{:url('Sellergoodsinfomessage/perfect_goods_list')}" />

                <?php } ?>
            </div>
        </form>
        <script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/ajaxfileupload.js" charset="utf-8"></script>
        <script src="{$Think.PLUGINS_SITE_ROOT}/jquery.ajaxContent.pack.js" type="text/javascript"></script>
        <script src="{$Think.HOME_SITE_ROOT}/js/sellergoods_add_step3.js"></script>
        <script src="{$Think.PLUGINS_SITE_ROOT}/layui/layui.js"></script>
    </div>
</div>
<script>
    goods_info = {:$json_goods_info};
    $('#all').click(function () {
        if ($(this).prop('checked')) {
            $(".bd-line").find('input:checkbox').prop("checked", true);
        } else {
            $(".bd-line").find('input:checkbox').prop("checked", false);
        }
    });

    //数组去重
    function unique(arr) {
        var ret = []

        for (var i = 0; i < arr.length; i++) {
            var item = arr[i]
            if (ret.indexOf(item) === -1) {
                ret.push(item)
            }
        }

        return ret
    }


    // 获得选中ID
    function getItems() {
        /* 获取选中的项 */
        var items = '';
        $('.checkboxs:checked').each(function () {
            items += this.value + ',';
        });
        if (items != '') {
            items = items.substr(0, (items.length - 1));
        } else {
            layer.alert('请选择要操作的数据', {icon: 2})
        }
        return items;
    }

    // 批量处理完善商品信息
    $('#p_w').click(function () {
        ids_str = getItems();
        if (ids_str) {
            perfect_goods(ids_str);
        }
    });

    //查看详情
    function goods_setting_info(ids_str) {
        _uri = HOMESITEURL + "/Sellergoodsadd/goods_setting_info?goods_id=" + ids_str;
        dsLayerOpen(_uri, '查看详情', '1200px', '680px');
    }

    // 完善商品信息
    function perfect_goods(ids_str) {
        _uri = HOMESITEURL + "/Sellergoodsadd/perfect_goods_content?commonid=" + ids_str;
        dsLayerOpen(_uri, '完善商品信息', '1200px', '680px');
    }

    /**
     * Layer 通用ifram弹出窗口
     */
    function dsLayerOpen(url, title, width, height) {
        if (!width) width = '900px';
        if (!height) height = '500px';
        layer.open({
            type: 2,
            title: title,
            area: [width, height],
            fixed: false, //不固定
            maxmin: true,
            content: url
        });
    }

    //全选
    $(document).on('click', '#checkboxs', function () {
        if ($(this).prop('checked')) {
            $(".top").find('input:checkbox').prop("checked", true);
        } else {
            $(".top").find('input:checkbox').prop("checked", false);
        }

    });

    //批量操作设置销售区域
    $(document).on('click', '#selectArea', function () {
        ids_str = getItems();
        var url = $(this).attr('action');
       // alert(url);return false;
        if (ids_str) {
            $(this).next().empty();
            id = $(this).parent().parent().attr('data-id');

            var input  = $('.list').find('input').each(function(){
                $(this).attr('class','aaa');
            });
            var $list = $('.list').html();
            $('.list').find('input').each(function () {
                $(this).attr('class', '');
            });

            /*  obj = $(this);*/
            layer.confirm('', {
                title: '销售区域选择',
                type: 1,
                area: ['900px', '750px'],
                fixed: false,
                maxmin: true,
                btn: ['确认', '取消'],
                content: $list,
            }, function () {
                var chk_values = [];
                var data_names = [];
                $('input[type="checkbox"]:checked').each(function () {
                    if ($(this).attr('class') != 'aaa') {
                        return true;
                    }
                    chk_values.push($(this).attr('ids'));
                    data_names.push($(this).attr('data_name'));
                });
                var area_name = unique(data_names);
                var chk_value = unique(chk_values);
                if (chk_value == '') {
                    layer.closeAll();
                } else {
                    $.ajax({
                        type: "POST",
                        url: url,
                        data:{ids:ids_str,area_name:area_name,area_ids:chk_value},
                        error: function (request) {
                            alert("Connection error");
                        },
                        success: function (result) {
                            if (result.code == 0) {
                                layer.closeAll();
                                layer.msg('设置销售区域成功');
                                parent.location.reload();
                                return false;
                            } else {
                                layer.closeAll();
                                layer.msg('设置销售区域失败');
                                parent.location.reload();
                                return false;
                            }
                        }
                    });
                    $(".top").find('input:checkbox').prop("checked", false);
                }
            });
        }

    });

    //单个操作设置销售区域
    $(document).on('click', '.save_area', function () {
        ids_str = $(this).attr('id');
        var url = $(this).attr('action');
            $(this).next().empty();
            id = $(this).parent().parent().attr('data-id');

            var input  = $('.list').find('input').each(function(){
                $(this).attr('class','aaa');
            });
            var $list = $('.list').html();
            $('.list').find('input').each(function () {
                $(this).attr('class', '');
            });

            layer.confirm('', {
                title: '销售区域选择',
                type: 1,
                area: ['750px', '600px'],
                fixed: false,
                maxmin: true,
                btn: ['确认', '取消'],
                content: $list,
            }, function () {
                var chk_values = [];
                var data_names = [];
                $('input[type="checkbox"]:checked').each(function () {
                    if ($(this).attr('class') != 'aaa') {
                        return true;
                    }
                    chk_values.push($(this).attr('ids'));
                    data_names.push($(this).attr('data_name'));
                });
                var area_name = unique(data_names);
                var chk_value = unique(chk_values);
                if (chk_value == '') {
                    layer.closeAll();
                } else {
                    $.ajax({
                        type: "POST",
                        url: url,
                        data:{ids:ids_str,area_name:area_name,area_ids:chk_value},
                        error: function (request) {
                            alert("Connection error");
                        },
                        success: function (result) {
                            if (result.code == 0) {
                                layer.closeAll();
                                layer.msg('设置销售区域成功');
                                parent.location.reload();
                                return false;
                            } else {
                                layer.closeAll();
                                layer.msg('设置销售区域失败');
                                parent.location.reload();
                                return false;
                            }
                        }
                    });
                    $(".top").find('input:checkbox').prop("checked", false);
                }
            });
    });


    //展开城市
    $(document).on('click', '.select_choose', function () {
        var choose_id = $(this).next().val();
        var obj = $(this);
        $.ajax({
            type: 'POST',
            url: "{:url('Sellergoodsadd/ajax_area')}",
            cache: false,
            data: {choose_id: choose_id},
            dataType: 'json',
            success: function (data) {
                var htmls = '';
                $.each(data, function (k, v) {
                    if (v.area_deep != 3) {
                        htmls += '<span class="select_choose select_choose dtreefont dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon"></span><input type="checkbox"  area_deep="' + v.area_deep + '" value="' + v.area_id + '" ids="' + v.last_ids + '" data_name="' + v.top_name + '" class="aaa" > ' + v.area_name + ' <div class="last' + v.area_id + '" style="padding-left:20px; margin:10px;"></div>';
                    } else {
                        htmls += '<i class="dtree-icon-shuye1 dtreefont-special dtree-theme-dtreefont dtree-theme-icon"></i>' + '<input type="checkbox" id="p3"  ids="' + v.area_id + '"    area_deep="' + v.area_deep + '" data_name="' + v.top_name + '" class="aaa" >' + v.area_name;
                    }
                });
                if ($(".sec" + choose_id).length > 0) {
                    $(".sec" + choose_id).html(htmls);
                    obj.attr("class", "area_hidden dtree-theme-dtreefont dtree-theme-ficon dtreefont dtree-icon-jian");
                } else {
                    $(".last" + choose_id).html(htmls);
                    obj.attr("class", "area_hidden dtree-theme-dtreefont dtree-theme-ficon dtreefont dtree-icon-jian");
                }
                if (obj.next().prop('checked')) {
                    obj.next().next().find('input:checkbox').prop('checked', true);
                } else {
                    $('input[name="checkall"]').prop('checked', false);
                    obj.next().next().find('input:checkbox').prop('checked', false);
                }
            }
        })


    });

    //关闭展开城市
    $(document).on('click', '.area_hidden', function () {
        var choose_id = $(this).next().val();
        $(".sec" + choose_id).empty();
        $(".last" + choose_id).empty();
        var obj = $(this);
        obj.attr("class", "select_choose dtree-icon-jia dtree-theme-dtreefont dtree-theme-ficon");
    });

    //点击地区复选框
    $(document).on('click', 'input:checkbox', function () {
        if ($(this).attr('area_deep') == 2) {
            if ($(this).prop('checked')) {
                $(this).next().find('input:checkbox').prop('checked', true);
            } else {
                $('input[name="checkall"]').prop('checked', false);
                //子集
                $(this).next().find('input:checkbox').prop('checked', false);
                //顶级
                $(this).parent('div').parent('div').find('input:checkbox:first').prop('checked', false);
            }
        } else if ($(this).attr('area_deep') == 1) {

            if ($(this).prop('checked')) {
                $(this).parent('div').find('input:checkbox').prop('checked', true);
            } else {
                $('input[name="checkall"]').prop('checked', false);
                $(this).parent('div').find('input:checkbox').prop('checked', false);
            }

        } else {

            if (!$(this).prop('checked')) {
                $('input[name="checkall"]').prop('checked', false);
                $(this).parent('div').parent('div').find('input:checkbox').first().prop('checked', false);
                $(this).parent('div').parent('div').parent('div').find('input:checkbox').first().prop('checked', false);
            }

        }
    });

   //下一步
   $('#button_submit').click(function(){

       for(let v of goods_info){
          if(v.area_id1==null || v.area==null){
              layer.msg('请补全信息后在再提交');return false;
          }
       }
       var url = $('form').attr('action');
       location.href = url;
   });

   //暂存
    $('#zan_submit').click(function(){

        for(let v of goods_info){
            if(v.area_id1==null || v.area==null){
                layer.msg('请补全信息后在再提交');return false;
            }
        }
        layer.msg('操作成功');
        var url = $(this).attr('action');
        location.href = url;
    });


</script>


{include file="default/base/mall_footer" /}